package com.haixiaoke.saas.enterprise.contract.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.fasc.open.api.v5_1.res.template.SignTemplateListInfo;
import com.fasc.open.api.v5_1.res.template.SignTemplateListRes;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.PageDomain;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.core.page.TableSupport;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.contract.domain.ContractTemplate;
import com.haixiaoke.saas.contract.fadada.service.FaDaDaService;
import com.haixiaoke.saas.contract.service.IContractTemplateService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 合同模板Controller
 *
 * @author panda
 * @date 2024-05-30
 */
@RestController
@RequestMapping("/enterprise/contract/contractTemplate")
public class EnterpriseContractTemplateController extends BaseController {
    @Autowired
    private IContractTemplateService contractTemplateService;
    @Autowired
    private FaDaDaService faDaDaService;

    /**
     * 查询合同模板列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contractTemplate:list')")
    @GetMapping("/page")
    @TenantScope
    public TableDataInfo page(ContractTemplate contractTemplate) {
        startPage();
        contractTemplate.setTenantId(TenantContext.getTenantId());
        List<ContractTemplate> list = contractTemplateService.selectContractTemplateList(contractTemplate);
        return getDataTable(list);
    }

    /**
     * 查询电子合同模板列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contractTemplate:list')")
    @GetMapping("/electronicVisa/page")
    public AjaxResult electronicVisaPage(@RequestParam(required = false) String templateName) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        SignTemplateListRes signTemplateList = faDaDaService.getSignTemplateList(pageDomain.getPageNum(), pageDomain.getPageSize(), templateName);
        // 创建一个SimpleDateFormat对象，定义目标日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (CollectionUtil.isNotEmpty(signTemplateList.getSignTemplates())) {
            for (SignTemplateListInfo signTemplateListInfo : signTemplateList.getSignTemplates()) {
                signTemplateListInfo.setCreateTime(sdf.format(Long.valueOf(signTemplateListInfo.getCreateTime())));
                signTemplateListInfo.setUpdateTime(sdf.format(Long.valueOf(signTemplateListInfo.getUpdateTime())));
            }
        }
        return success(signTemplateList);
    }

    /**
     * 查询合同模板列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contractTemplate:list')")
    @GetMapping("/listAndContent")
    @TenantScope
    public AjaxResult listAndContent(ContractTemplate contractTemplate) {
        contractTemplate.setTenantId(TenantContext.getTenantId());
        List<ContractTemplate> list = contractTemplateService.selectContractTemplateList(contractTemplate);
        return success(list);
    }

    /**
     * 查询合同模板下拉列表
     */
    @GetMapping("/list")
    @TenantScope
    public AjaxResult list(ContractTemplate contractTemplate) {
        contractTemplate.setTenantId(TenantContext.getTenantId());
        List<ContractTemplate> list = contractTemplateService.selectTemplateList(contractTemplate);
        return success(list);
    }

    /**
     * 获取合同模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contractTemplate:query')")
    @GetMapping(value = "/{contractTemplateId}")
    public AjaxResult getInfo(@PathVariable("contractTemplateId") String contractTemplateId) {
        return success(contractTemplateService.selectContractTemplateByContractTemplateId(contractTemplateId));
    }

    /**
     * 获取合同模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contractTemplate:query')")
    @GetMapping(value = "/placeholderSubstitution/{contractTemplateId}")
    public AjaxResult subGetInfo(@PathVariable("contractTemplateId") String contractTemplateId) {
        return success(contractTemplateService.selectContractByContractTemplateId(contractTemplateId));
    }

    /**
     * 预览合同模板内容
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contractTemplate:query')")
    @GetMapping(value = "/preview/{contractTemplateId}")
    public AjaxResult preview(@PathVariable("contractTemplateId") String contractTemplateId) {
        return success(contractTemplateService.previewContractTemplateByContractTemplateId(contractTemplateId));
    }

    /**
     * 预览合同模板内容
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contractTemplate:query')")
    @GetMapping(value = "/electronicVisa/preview/{signTemplateId}")
    public AjaxResult electronicVisaPreview(@PathVariable("signTemplateId") String signTemplateId) {
        return AjaxResult.success("操作成功", faDaDaService.getTemplatePreviewUrl(signTemplateId, null));
    }

    /**
     * 新增合同模板
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contractTemplate:add')")
    @Log(title = "合同模板", businessType = BusinessType.INSERT)
    @PostMapping
    @TenantScope
    public AjaxResult add(@RequestBody ContractTemplate contractTemplate) {
        contractTemplate.setTenantId(TenantContext.getTenantId());
        contractTemplate.setCreateBy(getUsername());
        return toAjax(contractTemplateService.insertContractTemplate(contractTemplate));
    }

    /**
     * 修改合同模板
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contractTemplate:edit')")
    @Log(title = "合同模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContractTemplate contractTemplate) {
        contractTemplate.setUpdateBy(getUsername());
        return toAjax(contractTemplateService.updateContractTemplate(contractTemplate));
    }

    /**
     * 修改合同模板内容
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contractTemplate:edit')")
    @Log(title = "合同模板", businessType = BusinessType.UPDATE)
    @PutMapping("/edit/content")
    public AjaxResult editContent(@RequestBody ContractTemplate contractTemplate) {
        contractTemplate.setUpdateBy(getUsername());
        return toAjax(contractTemplateService.updateContractTemplateContent(contractTemplate));
    }

    /**
     * 删除合同模板
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contractTemplate:remove')")
    @Log(title = "合同模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{contractTemplateIds}")
    public AjaxResult remove(@PathVariable String[] contractTemplateIds) {
        return toAjax(contractTemplateService.softDeleteContractTemplateByContractTemplateIds(contractTemplateIds));
    }
}
